package com.xywy.wjk;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.FragmentMainActivity;
import com.xywy.common.app.MainApplication;
import com.xywy.common.bean.ConnectedDevice;
import com.xywy.common.util.CalendarUtil;
import com.xywy.common.util.FileUtil;
import com.xywy.common.util.ble.util.BLEBloodPresureProvider;
import com.xywy.common.util.ble.util.BLEWeightScaleProvider;
import com.xywy.common.widget.TitleBar2;
import com.xywy.wjk.observer.ObserverConstants;
import com.xywy.wjk.util.DeviceState;
import com.xywy.wjk.util.WjkConstant;
import com.xywy.wjk.widget.TipDialog;
import com.xywy.wjk.widget.TipDialog.CancleListener;
import com.xywy.wjk.widget.TipDialog.ConfirmListener;
import com.xywy.wjk.widget.TipDialog.OkayListener;

/**
 * @ClassName: MyDeviceActivity
 * @Description: 我的设备
 * 
 * @author ZhiluCui
 * @date 2014年10月23日 上午9:56:05
 * @version:1.0
 */
public class MyDeviceActivity extends BaseActivity implements OnClickListener,
		OnLongClickListener {

	private TitleBar2 titleBar;
	private TextView tvBack;
	private TextView tvGo;
	private ImageView iv_none_device;
	private LinearLayout ll_content;
	private LinearLayout rlWeight; // 体重秤布局；
	private LinearLayout rlBloodPresure; // 血压计布局
	private LinearLayout rlBloodSugar; // 血糖布局；
	private LinearLayout rlTemputerSugar; // 体温布局；
	private LinearLayout[] lls;

	private SharedPreferences sp;
	private ImageView iv_weight_choose; // 体重对勾
	private ImageView iv_blood_presure_choose; // 血压对勾
	private ImageView iv_blood_sugar_choose;// 血糖对勾
	private ImageView iv_temputer_choose;
	private ImageView[] ivs;

	private TextView tvBloodTime;
	private TextView tvWeightTime;
	private TextView tvTemputerTime;
	private BluetoothManager mBluetoothManager;
	private BluetoothAdapter mBluetoothAdapter;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_device);
		MainApplication.addSpeciall(this);
		initView();
		this.preInit();
		
		mBluetoothManager = (BluetoothManager) context
				.getSystemService(Context.BLUETOOTH_SERVICE);
		mBluetoothAdapter = mBluetoothManager.getAdapter();
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.showConnectedDevice();
		this.loadData();
	}

	@Override
	public void preInit() {
		initBLE();
		/* 展示当前已经连接过的设备 */

	}
     ConnectedDevice weight_device ;
     ConnectedDevice blood_device ;
     ConnectedDevice temputer_device ;
	private void showConnectedDevice() {
		weight_device = FileUtil.getWeightDevice();
		blood_device = FileUtil.getBloodDevice();
		temputer_device = FileUtil.getTemputerDevice();
		ll_content.setVisibility(View.VISIBLE);
		if(weight_device.getConnect_time() >0){
			rlWeight.setVisibility(View.VISIBLE);
			if(weight_device.isConnected()){
				iv_weight_choose.setVisibility(View.VISIBLE);
				rlWeight.setClickable(false);
				rlWeight.setEnabled(false);
			}else {
				rlWeight.setClickable(true);
				rlWeight.setEnabled(true);
				iv_weight_choose.setVisibility(View.INVISIBLE);
			}
			tvWeightTime.setText(CalendarUtil.getDiffTime(weight_device.getConnect_time()));
		}else {
			rlWeight.setVisibility(View.GONE);
		}
		
		if(blood_device.getConnect_time() >0){
			rlBloodPresure.setVisibility(View.VISIBLE);
			if(blood_device.isConnected()){
				iv_blood_presure_choose.setVisibility(View.VISIBLE);
				rlBloodPresure.setClickable(false);
				rlBloodPresure.setEnabled(false);
			}else {
				iv_blood_presure_choose.setVisibility(View.INVISIBLE);
				rlBloodPresure.setClickable(true);
				rlBloodPresure.setEnabled(true);
			}
			tvBloodTime.setText(CalendarUtil.getDiffTime(blood_device.getConnect_time()));
		}else {
			rlBloodPresure.setVisibility(View.GONE);
		}
		
		if(temputer_device.getConnect_time() >0){
			rlTemputerSugar.setVisibility(View.VISIBLE);
			if(temputer_device.isConnected()){
				iv_temputer_choose.setVisibility(View.VISIBLE);
				rlTemputerSugar.setClickable(false);
				rlTemputerSugar.setEnabled(false);
			}else {
				iv_temputer_choose.setVisibility(View.INVISIBLE);
				rlTemputerSugar.setClickable(true);
				rlTemputerSugar.setEnabled(true);
			}
			tvTemputerTime.setText(CalendarUtil.getDiffTime(blood_device.getConnect_time()));
		}else {
			rlTemputerSugar.setVisibility(View.GONE);
		}
		
		if(blood_device.getConnect_time() == 0 && weight_device.getConnect_time() ==0 && temputer_device.getConnect_time() ==0){
			ll_content.setVisibility(View.GONE);
		}
		
		
//		if(blood_device.isConnected() && weight_device.isConnected()){
//			rlWeight.setClickable(true);
//			rlWeight.setEnabled(true);
//			iv_weight_choose.setVisibility(View.INVISIBLE);
//		}
	}

	@Override
	public void loadData() {
//			iv_weight_choose.setVisibility(View.VISIBLE);
//			rlWeight.setClickable(true);
			rlWeight.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					final TipDialog dialog = new TipDialog(
							MyDeviceActivity.this, TipDialog.VIEW_DIALOG);
					dialog.setTitleAndContent("连接设备", "确认连接到该设备吗？");
					dialog.setConfirmCallback(new ConfirmListener() {

						@Override
						public void confirm() {
							dialog.dismiss();
							MobclickAgent
									.onEvent(MyDeviceActivity.this, "A001");
							if(!mBluetoothAdapter.isEnabled()){
								unOpenBleTip();
								return;
							}
//							if(blood_device.isConnected()){//断开血压连接体重
//								Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION_DISCONNECT);
//								sendBroadcast(intent);
//							}else { //直接连接体重
//								Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION);
//								sendBroadcast(intent);
//							}
							dialogConfirm(WEIGHT_SCALE);
//							MainApplication.finishSpeciall();
						}
					});
					dialog.setCancleCallback(new CancleListener() {

						@Override
						public void cancle() {
							// TODO Auto-generated method stub
							dialog.dismiss();
						}
					});
					dialog.show();
				}
			});
			rlTemputerSugar.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					final TipDialog dialog = new TipDialog(
							MyDeviceActivity.this, TipDialog.VIEW_DIALOG);
					dialog.setTitleAndContent("连接设备", "确认连接到该设备吗？");
					dialog.setConfirmCallback(new ConfirmListener() {
						
						@Override
						public void confirm() {
							dialog.dismiss();
							MobclickAgent
							.onEvent(MyDeviceActivity.this, "A001");
							if(!mBluetoothAdapter.isEnabled()){
								unOpenBleTip();
								return;
							}
//							if(blood_device.isConnected()){//断开血压连接体重
//								Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION_DISCONNECT);
//								sendBroadcast(intent);
//							}else { //直接连接体重
//								Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION);
//								sendBroadcast(intent);
//							}
							dialogConfirm(TEMP_);
//							MainApplication.finishSpeciall();
						}
					});
					dialog.setCancleCallback(new CancleListener() {
						
						@Override
						public void cancle() {
							// TODO Auto-generated method stub
							dialog.dismiss();
						}
					});
					dialog.show();
				}
			});
//			iv_blood_presure_choose.setVisibility(View.VISIBLE);
//			rlBloodPresure.setClickable(true);
			rlBloodPresure.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					final TipDialog dialog = new TipDialog(
							MyDeviceActivity.this, TipDialog.VIEW_DIALOG);
					dialog.setTitleAndContent("连接设备", "确认连接该设备吗？");
					dialog.setConfirmCallback(new ConfirmListener() {

						@Override
						public void confirm() {
							// TODO Auto-generated method stub
							// 如果正在连接当前设备，则给出提示，不处理
							dialog.dismiss();
							
							if(!mBluetoothAdapter.isEnabled()){
								unOpenBleTip();
								return;
							}
//							if(weight_device.isConnected()){ //断开体重连接血压
//								Intent intent = new Intent(WjkConstant.CONNECT_BLOOD_ACTION_DISCONNET);
//								sendBroadcast(intent);
//							}else {//直接连接血压
//								Intent intent = new Intent(WjkConstant.CONNECT_BLOOD_ACTION);
//								sendBroadcast(intent);
//							}
							dialogConfirm(BLOOD_PRESURE);
//							MainApplication.finishSpeciall();
						}
					});
					dialog.setCancleCallback(new CancleListener() {

						@Override
						public void cancle() {
							// TODO Auto-generated method stub

							dialog.dismiss();
						}
					});
					dialog.show();
				}
			});
		
	}

	/**
	 * 初始化蓝牙配置
	 */
	private void initBLE() {
		MainApplication app = MainApplication.getInstance();

	}

	@Override
	public void initView() {
		// title
		titleBar = (TitleBar2) findViewById(R.id.activity_device_type_add_title);
		TextView tvTitle = titleBar.getTitle();
		tvBack = titleBar.getTvBack();
		tvGo = titleBar.getGo();
		// tvGo.setVisibility(View.GONE);
		tvGo.setText(getString(R.string.find_remind_add));
		tvGo.setOnClickListener(this);
		// tvGo.setVisibility(View.GONE);
		tvTitle.setText(getString(R.string.me_my_device));
		tvBack.setText(getString(R.string.tv_start_back));
		iv_none_device = (ImageView) findViewById(R.id.iv_none_device);
		ll_content = (LinearLayout) findViewById(R.id.ll_content);
		tvBack.setOnClickListener(this);
		rlWeight = (LinearLayout) findViewById(R.id.rl_weight);
		rlWeight.setOnClickListener(this);
		rlWeight.setOnLongClickListener(this);
		rlBloodPresure = (LinearLayout) findViewById(R.id.rl_blood_presure);
		rlBloodPresure.setOnClickListener(this);
		rlBloodPresure.setOnLongClickListener(this);
		rlBloodSugar = (LinearLayout) findViewById(R.id.rl_blood_sugar);
		rlBloodSugar.setOnClickListener(this);
		rlBloodSugar.setOnLongClickListener(this);

		lls = new LinearLayout[] { rlWeight, rlBloodPresure, rlBloodSugar };

		iv_weight_choose = (ImageView) findViewById(R.id.iv_weight_choose); // 体重对勾
		iv_blood_presure_choose = (ImageView) findViewById(R.id.iv_blood_presure_choose); // 血压对勾
		iv_blood_sugar_choose = (ImageView) findViewById(R.id.iv_blood_sugar_choose);
		ivs = new ImageView[] { iv_weight_choose, iv_blood_presure_choose,
				iv_blood_sugar_choose };
		tvBloodTime = (TextView) findViewById(R.id.bloodTime);
		tvWeightTime = (TextView) findViewById(R.id.weightTime);
		
		rlTemputerSugar = (LinearLayout) findViewById(R.id.rl_temputer);
		iv_temputer_choose = (ImageView) findViewById(R.id.iv_temputer_choose);
		tvTemputerTime = (TextView) findViewById(R.id.tv_temputer_time);

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub

		switch (view.getId()) {
		case R.id.tv_title_back:
			this.finish();
			break;
		case R.id.tv_title_go:
			MobclickAgent.onEvent(this, "A003");
			Intent addIntent = new Intent(this, AddDevicesActivity.class);
			startActivity(addIntent);
			// finish();
			break;

		case R.id.rl_blood_sugar:
			// 连接血糖仪
			// Intent sugarIntent = new Intent();
			// sugarIntent.putExtra("device", 2);
			// setResult(RESULT_OK, sugarIntent);
			// finish();
			break;

		}

	}

	private void unOpenBleTip() {
		final TipDialog dialog = new TipDialog(this, TipDialog.VIEW_TIP);
		dialog.setTitleAndContent("蓝牙未开启", "检测到你没有打开手机蓝牙，请打开后再试");
		dialog.setOkayCallback(new OkayListener() {

			@Override
			public void okay() {
				dialog.dismiss();
				Log.d(TAG, "用户未开启当前手机的蓝牙设备；");
			}
		});
		dialog.show();
	}

	@Override
	public boolean onLongClick(View view) {
		switch (view.getId()) {
		case R.id.rl_weight:

			break;
		case R.id.rl_blood_presure:
			break;
		case R.id.rl_blood_sugar:
			break;
		}
		return false;
	}
	
	
	private int WEIGHT_SCALE = 0;
	private int TEMP_ = 1;
	private int BLOOD_PRESURE = 2;
	public void dialogConfirm(int cur_device) {
		ConnectedDevice current_device = FileUtil.getCurrentDevice();
		//蓝牙未开启
		if(current_device.isConnected()){
			if(cur_device ==WEIGHT_SCALE ){
				Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION_DISCONNECT);
				sendBroadcast(intent);
				MainApplication.finishSpeciall();
			}else if(cur_device == BLOOD_PRESURE){
				Intent intent = new Intent(WjkConstant.CONNECT_BLOOD_ACTION_DISCONNET);
				sendBroadcast(intent);
				MainApplication.finishSpeciall();
			}else if(cur_device == TEMP_){
				Intent intent = new Intent(WjkConstant.CONNECT_TEMPUT_ACTION_DISCONNECT);
				sendBroadcast(intent);
				MainApplication.finishSpeciall();
//				Toast.makeText(SelectBrandActivity.this,
//						"暂无体温设备", Toast.LENGTH_LONG).show();
			}
		}else {
			if(cur_device ==WEIGHT_SCALE ){
				Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION);
				sendBroadcast(intent);
				MainApplication.finishSpeciall();
			}else if(cur_device == BLOOD_PRESURE){
				Intent intent = new Intent(WjkConstant.CONNECT_BLOOD_ACTION);
				sendBroadcast(intent);
				MainApplication.finishSpeciall();
			}else if(cur_device == TEMP_){
//				Toast.makeText(SelectBrandActivity.this,
//						"暂无体温设备", Toast.LENGTH_LONG).show();
				Intent intent = new Intent(WjkConstant.CONNECT_TEMPUT_ACTION);
				sendBroadcast(intent);
				MainApplication.finishSpeciall();
			}
		}
		
	}

}
